package com.koushikdutta.async.http;

import android.net.Uri;
import android.util.Log;
import com.koushikdutta.async.AsyncSSLException;
import com.koushikdutta.async.http.body.AsyncHttpRequestBody;
import com.koushikdutta.async.http.libcore.RawHeaders;
import com.koushikdutta.async.http.libcore.RequestHeaders;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;

public class AsyncHttpRequest {
    static final /* synthetic */ boolean $assertionsDisabled = (!AsyncHttpRequest.class.desiredAssertionStatus());
    String LOGTAG;
    long executionTime;
    int logLevel;
    private AsyncHttpRequestBody mBody;
    private boolean mFollowRedirect;
    private RequestHeaders mHeaders;
    private String mMethod;
    private RawHeaders mRawHeaders;
    int mTimeout;
    String proxyHost;
    int proxyPort;

    public RequestLine getRequestLine() {
        return new RequestLine() {
            public String getUri() {
                return AsyncHttpRequest.this.getUri().toString();
            }

            public ProtocolVersion getProtocolVersion() {
                return new ProtocolVersion("HTTP", 1, 1);
            }

            public String getMethod() {
                return AsyncHttpRequest.this.mMethod;
            }

            public String toString() {
                String path = AsyncHttpRequest.this.getUri().getEncodedPath();
                if (path == null || path.length() == 0) {
                    path = "/";
                }
                String query = AsyncHttpRequest.this.getUri().getEncodedQuery();
                if (!(query == null || query.length() == 0)) {
                    path = path + "?" + query;
                }
                return String.format("%s %s HTTP/1.1", new Object[]{AsyncHttpRequest.this.mMethod, path});
            }
        };
    }

    public RequestLine getProxyRequestLine() {
        return new RequestLine() {
            public String getUri() {
                return AsyncHttpRequest.this.getUri().toString();
            }

            public ProtocolVersion getProtocolVersion() {
                return new ProtocolVersion("HTTP", 1, 1);
            }

            public String getMethod() {
                return AsyncHttpRequest.this.mMethod;
            }

            public String toString() {
                return String.format("%s %s HTTP/1.1", new Object[]{AsyncHttpRequest.this.mMethod, AsyncHttpRequest.this.getUri()});
            }
        };
    }

    protected static String getDefaultUserAgent() {
        String agent = System.getProperty("http.agent");
        return agent != null ? agent : "Java" + System.getProperty("java.version");
    }

    public String getMethod() {
        return this.mMethod;
    }

    public AsyncHttpRequest(Uri uri, String method) {
        this(uri, method, null);
    }

    public static void setDefaultHeaders(RawHeaders ret, Uri uri) {
        if (uri != null) {
            String host = uri.getHost();
            if (uri.getPort() != -1) {
                host = host + ":" + uri.getPort();
            }
            if (host != null) {
                ret.set("Host", host);
            }
        }
        ret.set("User-Agent", getDefaultUserAgent());
        ret.set("Accept-Encoding", "gzip, deflate");
        ret.set("Connection", "keep-alive");
        ret.set("Accept", "*/*");
    }

    public AsyncHttpRequest(Uri uri, String method, RawHeaders headers) {
        this.mRawHeaders = new RawHeaders();
        this.mFollowRedirect = true;
        this.mTimeout = 30000;
        this.proxyPort = -1;
        if ($assertionsDisabled || uri != null) {
            this.mMethod = method;
            if (headers == null) {
                this.mRawHeaders = new RawHeaders();
            } else {
                this.mRawHeaders = headers;
            }
            if (headers == null) {
                setDefaultHeaders(this.mRawHeaders, uri);
            }
            this.mHeaders = new RequestHeaders(uri, this.mRawHeaders);
            this.mRawHeaders.setStatusLine(getRequestLine().toString());
            return;
        }
        throw new AssertionError();
    }

    public Uri getUri() {
        return this.mHeaders.getUri();
    }

    public RequestHeaders getHeaders() {
        return this.mHeaders;
    }

    public String getRequestString() {
        return this.mRawHeaders.toHeaderString();
    }

    public boolean getFollowRedirect() {
        return this.mFollowRedirect;
    }

    public AsyncHttpRequest setFollowRedirect(boolean follow) {
        this.mFollowRedirect = follow;
        return this;
    }

    public void setBody(AsyncHttpRequestBody body) {
        this.mBody = body;
    }

    public AsyncHttpRequestBody getBody() {
        return this.mBody;
    }

    public void onHandshakeException(AsyncSSLException e) {
    }

    public int getTimeout() {
        return this.mTimeout;
    }

    public AsyncHttpRequest setTimeout(int timeout) {
        this.mTimeout = timeout;
        return this;
    }

    public void enableProxy(String host, int port) {
        this.proxyHost = host;
        this.proxyPort = port;
    }

    public String getProxyHost() {
        return this.proxyHost;
    }

    public int getProxyPort() {
        return this.proxyPort;
    }

    public void setLogging(String tag, int level) {
        this.LOGTAG = tag;
        this.logLevel = level;
    }

    private String getLogMessage(String message) {
        long elapsed;
        if (this.executionTime != 0) {
            elapsed = System.currentTimeMillis() - this.executionTime;
        } else {
            elapsed = 0;
        }
        return String.format("(%d ms) %s: %s", new Object[]{Long.valueOf(elapsed), getUri(), message});
    }

    public void logi(String message) {
        if (this.LOGTAG != null && this.logLevel <= 4) {
            Log.i(this.LOGTAG, getLogMessage(message));
        }
    }

    public void logv(String message) {
        if (this.LOGTAG != null && this.logLevel <= 2) {
            Log.v(this.LOGTAG, getLogMessage(message));
        }
    }

    public void logd(String message) {
        if (this.LOGTAG != null && this.logLevel <= 3) {
            Log.d(this.LOGTAG, getLogMessage(message));
        }
    }

    public void loge(String message, Exception e) {
        if (this.LOGTAG != null && this.logLevel <= 6) {
            Log.e(this.LOGTAG, getLogMessage(message));
            Log.e(this.LOGTAG, e.getMessage(), e);
        }
    }
}
